#include "device_mdle.h"
#include "ui_device_mdle.h"
#include <QStandardItemModel>
#include <QDebug>
device_mdle::device_mdle(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::device_mdle)
{
    ui->setupUi(this);
    table = new QTableWidgetSave(8,6,this); //! 创建器件
    ui->layout_table->addWidget(table);
    // --------------------------------------------
    //创建器件的基础属性
    table->setItem(1-1, 1-1, new QTableWidgetItem(QString("dev_name")));
    table->setItem(1-1, 2-1, new QTableWidgetItem(QString("devive_mdle")));
    table->setItem(2-1, 1-1, new QTableWidgetItem(QString("dev_code")));
    table->setItem(2-1, 2-1, new QTableWidgetItem(QString("00000000")));
    table->setItem(3-1, 1-1, new QTableWidgetItem(QString("site_x0")));
    table->setItem(3-1, 2-1, new QTableWidgetItem(QString("5")));
    table->setItem(4-1, 1-1, new QTableWidgetItem(QString("site_y0")));
    table->setItem(4-1, 2-1, new QTableWidgetItem(QString("15")));
    table->setItem(5-1, 1-1, new QTableWidgetItem(QString("site_x1")));
    table->setItem(5-1, 2-1, new QTableWidgetItem(QString("10")));
    table->setItem(6-1, 1-1, new QTableWidgetItem(QString("site_y1")));
    table->setItem(6-1, 2-1, new QTableWidgetItem(QString("20")));
    table->setItem(7-1, 1-1, new QTableWidgetItem(QString("dev_handle")));
    table->setItem(7-1, 2-1, new QTableWidgetItem(QString("devive_mdle0")));
    // --------------------------------------------
    // 增加IO属性
    table->setItem(1-1, 3-1, new QTableWidgetItem(QString("itf_in_num")));
    table->setItem(1-1, 4-1, new QTableWidgetItem(QString("1")));
    table->setItem(2-1, 3-1, new QTableWidgetItem(QString("itf_in1")));
    table->setItem(2-1, 4-1, new QTableWidgetItem(QString("interface_mdle_in")));

    table->setItem(1-1, 5-1, new QTableWidgetItem(QString("itf_out_num")));
    table->setItem(1-1, 6-1, new QTableWidgetItem(QString("1")));
    table->setItem(2-1, 5-1, new QTableWidgetItem(QString("itf_out1")));
    table->setItem(2-1, 6-1, new QTableWidgetItem(QString("interface_mdle_out"))); //器件保留的IO信息，IO根据名字直接加载

}

device_mdle::~device_mdle()
{
    delete ui;
}

QString device_mdle::getDevName()
{
    return table->item(1-1,2-1)->text();
}

QString device_mdle::getDevCode()
{
    return table->item(2-1,2-1)->text();
}

QList<int> device_mdle::getSite()
{
    QList<int> site;
    site.append(table->item(3-1,2-1)->text().toInt());
    site.append(table->item(4-1,2-1)->text().toInt());
    site.append(table->item(5-1,2-1)->text().toInt());
    site.append(table->item(6-1,2-1)->text().toInt());
    return site;
}

QList<int> device_mdle::getSize()
{
    QList<int> size;
    size.append(table->item(5-1,2-1)->text().toInt() - table->item(3-1,2-1)->text().toInt()); // 获取X宽度
    size.append(table->item(6-1,2-1)->text().toInt() - table->item(4-1,2-1)->text().toInt()); // 获取Y宽度
    return size;
}

QString device_mdle::getHandle()
{
    return table->item(7-1,2-1)->text();
}

QList<QString> device_mdle::getInputName()
{
    QList<QString> ins; //获取所有输入
    for (int i = 1; i <= table->item(1-1,4-1)->text().toInt(); i ++) //获取所有输入个数
    {
        ins.append(table->item(i,4-1)->text());
    }
    return ins;
}

QList<QString> device_mdle::getOutputName()
{
    QList<QString> outs; //获取所有输出端口
    for (int i = 1; i <= table->item(1-1,6-1)->text().toInt(); i ++) //获取所有输入端口
    {
        outs.append(table->item(i,6-1)->text());
    }
    return outs;
}

void device_mdle::setDevName(QString dev_name)
{
    table->item(1-1,2-1)->setText(dev_name);
}

void device_mdle::setDevCode(QString dev_code)
{
    table->item(2-1,2-1)->setText(dev_code);
}

void device_mdle::setHandle(QString dev_handle)
{
    table->item(7-1,2-1)->setText(dev_handle);
}

void device_mdle::setSite(int x0, int y0, int x1, int y1)
{
    table->item(3-1,2-1)->setText(QString::number(x0));
    table->item(4-1,2-1)->setText(QString::number(y0));
    table->item(5-1,2-1)->setText(QString::number(x1));
    table->item(6-1,2-1)->setText(QString::number(y1));
}

QList<QString> device_mdle::getDate()
{
    QList<QString> data;
    for(int i = 0; i < table->rowCount(); i ++)
    {
        for(int j = 0; j < table->columnCount(); j ++)
        {
            QTableWidgetItem* sourceItem = table->item(i, j);
            if(sourceItem) //单元格存在
            {
                data.append(sourceItem->text());
            }
            else
            {
                data.append("");
            }

        }
    }
    //qDebug() << data;
    return data;
}

void device_mdle::setDate(QList<QString> data)
{
    for(int i = 0; i < table->rowCount(); i ++)
    {
        for(int j = 0; j < table->columnCount(); j ++)
        {
            //qDebug() << i  << j << data.at(i * table->columnCount() + j);
            if(table->item(i, j) != nullptr)
            {
                table->item(i,j)->setText(data.at(i * table->columnCount() + j));
            }
            else //创建空的配置
            {
                QTableWidgetItem* sourceItem = new QTableWidgetItem("");
                table->setItem(i,j,sourceItem);
            }

        }
    }
}

void device_mdle::save(QString &fileName)
{
    table->exportToCsv(fileName);
}

void device_mdle::load(QString &fileName)
{
    table->importFromCsv(fileName);
}

void device_mdle::hide_dev(bool en)
{
    table->setVisible(!en);
}

void device_mdle::signal_itf_monitor_slot(QVariantList & itf)
{
    QDEBUG << QDEBUG_WITH_PARAM(itf);
}

